package io.renren.modules.ossupload.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import io.renren.common.exception.RRException;
import io.renren.modules.ossupload.service.OssUploadService;
import io.renren.modules.ossupload.utils.ConstanPropertiesUtils;
import io.renren.modules.ossupload.vo.PictureInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OssUploadServiceImpl implements OssUploadService {

    @Override
    public List<PictureInfoVO> uploadFileAvator(List<MultipartFile> uploadFile) {
        log.info("图片OSS上传(批量)");

        String endpoint = ConstanPropertiesUtils.END_POIND;
        String accessKeyId = ConstanPropertiesUtils.KEY_ID;
        String accessKeySecret = ConstanPropertiesUtils.KEY_SECRET;
        String bucketName = ConstanPropertiesUtils.BUCKET_NAME;

        List<PictureInfoVO> infoVOList = new ArrayList<>();
        for (MultipartFile file : uploadFile) {
            try {
                //获取文件原始名称
                String oldFilename = file.getOriginalFilename();
                //获取后缀名
                String prefix = FilenameUtils.getExtension(oldFilename);
                String newPrefix = prefix.toLowerCase();
                //判断文件名是否合法
                String jpg = "jpg";
                String png = "png";
                String pneg = "pneg";
                String jpeg = "jpeg";
                if (!jpg.equals(newPrefix) && !png.equals(newPrefix) && !pneg.equals(newPrefix) && !jpeg.equals(newPrefix)) {
                    throw new RRException("Upload file format error");
                }
                // 对文件重新命名
                String uuid = UUID.randomUUID().toString().replaceAll("-","");
                String newFileName = uuid + "." + prefix;
                log.info("============新文件名============" + newFileName);
                //把文件按照日期进行分类
                String datePath = new DateTime().toString("yyyyMMdd");
                newFileName = datePath + "/" + newFileName;
                // 创建OSSClient实例
                OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                // 获取上传文件输入流
                InputStream inputStream = inputStream = file.getInputStream();
                // 调用oss方法实现上传 第一个参数：bucketName  第二个参数：上传到oss文件路径和文件名称
                log.info("============上传图片,开始上传============");
                ossClient.putObject(bucketName, newFileName, inputStream);
                // 关闭OSSClient
                ossClient.shutdown();
                //把上传后的路径返回
                String url = "https://" + bucketName + "." + endpoint + "/" + newFileName;

                PictureInfoVO pictureInfoVo = new PictureInfoVO();
                pictureInfoVo.setImgAddress(url);
                pictureInfoVo.setImgName(newFileName);
                infoVOList.add(pictureInfoVo);
                return infoVOList;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
